/*
    Matali Physics Demo
    Copyright (c) 2011 KOMIRES Sp. z o. o.
 */
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Komires.MataliPhysics;

namespace MataliPhysicsDemo
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class MenuDraw1
    {
        Demo demo;
        PhysicsScene scene;
        string instanceIndexName;

        PhysicsObject infoScreen;
        PhysicsObject infoDescription;

        Color fontDrawColor;

        Vector3 position;
        Matrix projection;
        Matrix world;
        Matrix view;

        public MenuDraw1(Demo demo, int instanceIndex)
        {
            this.demo = demo;
            instanceIndexName = " " + instanceIndex.ToString();

            fontDrawColor = Color.White;

            world = Matrix.Identity;
        }

        public void Initialize(PhysicsScene scene)
        {
            this.scene = scene;
        }

        public void SetControllers()
        {
            infoScreen = scene.Factory.PhysicsObjectManager.Find("Info Screen" + instanceIndexName);
            infoDescription = scene.Factory.PhysicsObjectManager.Find("Info Description" + instanceIndexName);

            PhysicsObject objectBase = scene.Factory.PhysicsObjectManager.Find("Info" + instanceIndexName);

            if (objectBase != null)
                objectBase.UserControllers.DrawMethods += new DrawMethod(Draw);
        }

        void Draw(DrawMethodArgs args)
        {
            PhysicsScene scene = demo.Engine.Factory.PhysicsSceneManager.Get(args.OwnerSceneIndex);
            PhysicsObject objectBase = scene.Factory.PhysicsObjectManager.Get(args.OwnerIndex);

            PhysicsObject physicsObjectWithCamera = scene.GetPhysicsObjectsWithCamera(0);

            if (physicsObjectWithCamera == null) return;

            PhysicsCamera activeCamera = physicsObjectWithCamera.Camera;

            if (activeCamera == null) return;

            float time = args.Time;

            if ((infoScreen == null) || (infoDescription == null)) return;
            if (!infoDescription.EnableDrawing) return;

            string sceneScreenName = infoScreen.Material.UserTagStr;

            if (sceneScreenName == null) return;
            if (!demo.Descriptions.ContainsKey(sceneScreenName)) return;

            List<string> Descriptions = demo.Descriptions[sceneScreenName];

            string info = null;

            infoDescription.MainWorldTransform.GetPosition(ref position);
            activeCamera.View.GetViewMatrix(ref view);
            activeCamera.Projection.GetProjectionMatrix(ref projection);

            demo.DemoFont3D.Effect.View = view;
            demo.DemoFont3D.Effect.World = world;
            demo.DemoFont3D.Effect.Projection = projection;

            demo.DemoFont3D.Begin();

            for (int i = 0; i < Descriptions.Count; i++)
            {
                info = Descriptions[i];

                if (info != null)
                    demo.DemoFont3D.Draw(position.X - 25.0f, position.Y + 12.0f - 1.4f * i, position.Z - 0.5f, 0.08125f, 0.12125f, info, fontDrawColor);
            }

            demo.DemoFont3D.End();
        }
    }
}
